package com.isorf.controller;


import com.github.pagehelper.PageInfo;
import com.isorf.pojo.links;
import com.isorf.pojo.users;
import com.isorf.service.Linkservice;
import com.isorf.service.UploadService;
import com.isorf.util.responseresult;
import io.swagger.annotations.*;
import net.sf.jsqlparser.statement.execute.Execute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
public class linkcontroller {
    @Autowired
    private Linkservice linkservice;
    @Autowired
    private UploadService uploadService;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;





    @PostMapping("/updateLink")
    public responseresult<Boolean> updateLink(@RequestBody links link) {
        if (link == null || link.getId() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }
        Boolean result = linkservice.updateLink(link);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "更新成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "更新失败");
        }
    }



    @PostMapping("/addlink")
    public responseresult<Boolean> addlink(@RequestBody links link) {
        Boolean result = linkservice.addlink(link);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "Link successful", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "Link failed");
        }
        // Remove the unreachable statement
    }


    @PostMapping("/uploadImage")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(
                    name = "flimage",
                    value = "上传的图片文件",
                    required = true,
                    paramType = "formData",
                    dataType = "MultipartFile",
                    dataTypeClass = MultipartFile.class,
                    defaultValue = "")})
    public responseresult uploadImage(@RequestPart MultipartFile flimage) {
        String flag = uploadService.uploadImage(path, flimage);
        if (flag!=null) {
            return responseresult.success(responseresult.SUCCESS, "上传成功", flag);
        }else{
            return responseresult.failure(responseresult.FAILED, "上传失败");
        }
    }



    //分页查询
    @PostMapping("/pageList")
    public responseresult<PageInfo<links>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<links> result = linkservice.pageList(pageNum, pageSize);
        if (result==null&&result.getList().size()==0) {
            return responseresult.failure(responseresult.FAILED, "查询用户列表失败");
        }else {
            return responseresult.success(responseresult.SUCCESS,"查询成功", result);
        }
    }

    @PostMapping("/batchDelete")
public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids) {
    // 检查参数
    if (ids == null || ids.isEmpty()) {
        return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
    }

    // 调用service层的批量删除方法
    Boolean result = linkservice.batchDelete(ids);
    if (result) {
        return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
    } else {
        return responseresult.failure(responseresult.FAILED, "批量删除失败");
    }
}








@PostMapping("/deletelink")
public responseresult<Boolean> deletelink(@RequestBody links link) {
    // 检查link对象和id是否为空
    if (link == null || link.getId() == null) {
        return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
    }

    // 调用service层的删除方法，应该是一个删除方法而不是addlink
    Boolean result = linkservice.deletelink(link.getId());
    if (result) {
        return responseresult.success(responseresult.SUCCESS, "删除成功", result);
    } else {
        return responseresult.failure(responseresult.FAILED, "删除失败");
    }
}


}
